package cn.uniqueinfo.dp.controller;

import cn.uniqueinfo.dp.controller.base.BaseController;
import cn.uniqueinfo.dp.service.IMenuService;
import cn.uniqueinfo.dp.util.TreeMenuUtil;
import cn.uniqueinfo.dp.vo.EasyuiTableResult;
import cn.uniqueinfo.dp.vo.MenuVO;
import cn.uniqueinfo.dp.vo.TreeVO;
import cn.uniqueinfo.dp.vo.UserVO;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author:朱元勋
 * @mail:395910063@qq.com
 * @date:2019-01-30 16:40:31
 * @description:菜单表
 */
@Slf4j
@Controller
@RequestMapping(value = "/menu")
public class MenuController extends BaseController<MenuVO> {
    private IMenuService service;

    public MenuController(IMenuService service) {
        this.service = service;
        this.baseService = service;
        this.ctlName = "menu";
        this.tipName = "菜单管理";
    }

    @GetMapping("/tree")
    @ResponseBody
    public EasyuiTableResult<MenuVO> findTree() {
        EasyuiTableResult<MenuVO> tableResult = new EasyuiTableResult<>();
        List<MenuVO> rows = this.service.findAll(null);
        tableResult.setRows(rows);
        tableResult.setTotal((long) rows.size());
        return tableResult;
    }

    /**
     * @author:朱元勋
     * @date:2019/2/19 16:28
     * @description: 获取指定用户对应的角色菜单
     * @param:[]
     * @return:java.util.List<cn.uniqueinfo.dp.vo.MenuVO>
     */
    @PostMapping(value = "/um")
    @ResponseBody
    public List<TreeVO> findByUserId() {
        // 获取当前登录人信息
        UserVO user = (UserVO) SecurityUtils.getSubject().getPrincipal();
        String menuJson = TreeMenuUtil.getJson(this.service.findByUserId(user.getId()));
        List<TreeVO> treeList = JSONObject.parseArray(menuJson, TreeVO.class);
        return treeList;
    }
}
